﻿using AutoMapper;
using AutoMapper.QueryableExtensions;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GGetTestService.Extension
{
    public static class IMapperExtensions
    {
        /// <summary>
        /// 转为TEntity实体类型(需要首先添加实体之间的映射)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="list"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        public static IEnumerable<TEntity> ProjectTo<TEntity>(this object list, IMapper mapper)
        {
            return mapper.Map<IEnumerable<TEntity>>(list);
        }

        /// <summary>
        ///  转为TEntity实体类型(需要首先添加实体之间的映射)
        /// </summary>
        /// <typeparam name="OldEntity"></typeparam>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="list"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        public static IEnumerable<TEntity> ProjectTo<OldEntity, TEntity>(this IEnumerable<OldEntity> list, IMapper mapper)
        {
            return mapper.Map<IEnumerable<TEntity>>(list);
        }

        /// <summary>
        /// 转为TEntity实体类型(需要首先添加实体之间的映射)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="query"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> ProjectTo<TEntity>(this IQueryable query, IMapper mapper)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            return Extensions.ProjectTo<TEntity>(query, mapper.ConfigurationProvider);
        }
    }
}